﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MYSMRM.Dao.Base;


namespace MYSMRM.Manager.Base
{
    public abstract class ManagerBase<T> : IManager<T> where T : class
    {
        public IDao<T> CurrentDao { get; set; }

        public virtual T Get(object id)
        {
            if (id == null)
            {
                return null;
            }
            return this.CurrentDao.Get(id);
        }

        public virtual T Load(object id)
        {
            if (id == null)
            {
                return null;
            }
            return this.CurrentDao.Load(id);
        }

        public virtual object Save(T entity)
        {
            if (entity == null)
            {
                return null;
            }
            return this.CurrentDao.Save(entity);
        }

        public virtual void Update(T entity)
        {
            if (entity == null)
            {
                return;
            }

            this.CurrentDao.Update(entity);
        }

        public virtual void SaveOrUpdate(T entity)
        {
            if (entity == null)
            {
                return;
            }

            this.CurrentDao.SaveOrUpdate(entity);
        }

        public virtual void Delete(object id)
        {
            if (id == null)
            {
                return;
            }

            this.CurrentDao.Delete(id);
        }

        public virtual void Delete(IList<object> idList)
        {
            if (idList == null || idList.Count == 0)
            {
                return;
            }

            this.CurrentDao.Delete(idList);
        }

        public virtual IList<T> LoadAll()
        {
            return this.CurrentDao.LoadAll().ToList();
        }

        public virtual IList<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
        {
            return this.CurrentDao.LoadAllWithPage(out count, pageIndex, pageSize).ToList();
        }

        public virtual void ExecuteNonQuery(string sql)
        {
            if (sql.Trim() == "")
            {
                return;
            }
            this.CurrentDao.ExecuteNonQuery(sql);
        }

        public virtual DataSet ExecuteDataSet(string sql)
        {
            return this.CurrentDao.ExecuteDataSet(sql);
        }

        public virtual DataTable ExecuteDataTable(string sql)
        {
            return this.CurrentDao.ExecuteDataTable(sql);
        }
    }
}
